package com.muchun.base.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.muchun.base.entity.BaseEntityOne;
import com.muchun.base.service.BaseService;
import com.muchun.base.util.DateUtil;
import com.muchun.base.util.JSONUtils;
/**
 * baseController
 */
@Controller
@RequestMapping("/base")
@SuppressWarnings("all")
public class BaseController {

	private static final Logger LOGGER = Logger.getLogger(BaseController.class);
	
	@Autowired
	private BaseService baseService;
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public @ResponseBody String add(@RequestBody BaseEntityOne entity, Model model){
		LOGGER.info("新建数据 begin");
		entity.setCreateTime(DateUtil.getNowDateTime());
		entity.setLastUpdateTime(DateUtil.getNowDateTime());
		entity.setDataStatus(true);
		baseService.save(entity);
		LOGGER.info("新建数据 end");
		return "";
	}
	
	@RequestMapping(value="/get", method=RequestMethod.POST)
	public @ResponseBody String get(String id, Model model){
		LOGGER.info("获取数据");
		BaseEntityOne entity=(BaseEntityOne) baseService.get(BaseEntityOne.class, id);
		Map<String,Object> data=new HashMap<String, Object>();
		data.put("entity", entity);
		return JSONUtils.toJSONStringBySuccess(entity);
	}
	
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	public @ResponseBody String edit(@RequestBody BaseEntityOne entity, Model model){
		LOGGER.info("编辑数据");
		//交换数据
		entity.setLastUpdateTime(DateUtil.getNowDateTime());
		baseService.update(entity);
		return "";
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public @ResponseBody String delete(String id, Model model){
		LOGGER.info("删除数据");
		BaseEntityOne entity = new BaseEntityOne();
		entity.setId(id);
		baseService.delete(entity);
		return "";
	}
	
}
